package com.alibaba.otter.shared.arbitrate.autoconfigure;

import com.alibaba.otter.shared.arbitrate.ArbitrateManageService;
import com.alibaba.otter.shared.arbitrate.ArbitrateViewService;
import com.alibaba.otter.shared.arbitrate.impl.ArbitrateManageServiceImpl;
import com.alibaba.otter.shared.arbitrate.impl.manage.ChannelArbitrateEvent;
import com.alibaba.otter.shared.arbitrate.impl.manage.NodeArbitrateEvent;
import com.alibaba.otter.shared.arbitrate.impl.manage.PipelineArbitrateEvent;
import com.alibaba.otter.shared.arbitrate.impl.manage.SystemArbitrateEvent;
import com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.termin.ErrorTerminProcess;
import com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.termin.WarningTerminProcess;
import com.alibaba.otter.shared.arbitrate.impl.zookeeper.ZooKeeperHeartBeatWorker;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;

@Configuration(proxyBeanMethods = false)
public class ArbitrateManagerConfiguration {


    @Bean
    public ArbitrateManageService arbitrateManageService(SystemArbitrateEvent systemEvent, ChannelArbitrateEvent channelEvent, NodeArbitrateEvent nodeEvent, PipelineArbitrateEvent pipelineEvent) {
        ArbitrateManageServiceImpl arbitrateManageService = new ArbitrateManageServiceImpl();
        arbitrateManageService.setSystemEvent(systemEvent);
        arbitrateManageService.setChannelEvent(channelEvent);
        arbitrateManageService.setNodeEvent(nodeEvent);
        arbitrateManageService.setPipelineEvent(pipelineEvent);
        return arbitrateManageService;
    }

    @Bean
    public ChannelArbitrateEvent channelArbitrateEvent(ArbitrateViewService arbitrateViewService,
                                                       NodeArbitrateEvent nodeEvent,
                                                       ErrorTerminProcess errorTerminProcess,
                                                       WarningTerminProcess warningTerminProcess,
                                                       @Qualifier("arbitrateExecutor") ExecutorService arbitrateExecutor) {
        ChannelArbitrateEvent channelArbitrateEvent = new ChannelArbitrateEvent();
        channelArbitrateEvent.setArbitrateViewService(arbitrateViewService);
        channelArbitrateEvent.setNodeEvent(nodeEvent);
        channelArbitrateEvent.setErrorTerminProcess(errorTerminProcess);
        channelArbitrateEvent.setWarningTerminProcess(warningTerminProcess);
        channelArbitrateEvent.setArbitrateExecutor(arbitrateExecutor);
        return channelArbitrateEvent;
    }

    @Bean
    public PipelineArbitrateEvent pipelineArbitrateEvent() {
        return new PipelineArbitrateEvent();
    }

    @Bean
    public NodeArbitrateEvent nodeEvent() {
        return new NodeArbitrateEvent();
    }

    @Bean
    public SystemArbitrateEvent systemArbitrateEvent() {
        return new SystemArbitrateEvent();
    }


    @Bean
    public ZooKeeperHeartBeatWorker zooKeeperHeartBeatWorker(NodeArbitrateEvent nodeEvent) {
        ZooKeeperHeartBeatWorker zooKeeperHeartBeatWorker = new ZooKeeperHeartBeatWorker();
        zooKeeperHeartBeatWorker.setNodeEvent(nodeEvent);
        return zooKeeperHeartBeatWorker;
    }
}
